package com.xhsj.user.pms.patent.controller;

import com.xhsj.user.pms.base.BaseController;
import com.xhsj.user.pms.patent.domain.TemplateFile;
import com.xhsj.user.pms.patent.service.TemplateFileService;
import com.xhsj.user.utils.IdGen;
import com.xhsj.user.utils.Message;
import com.xhsj.user.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @Author: liuqinghua
 * @Description:专利模板
 * @Data:2020/2/26
 */
@Slf4j
@Api(value = "专利模板" ,description = "专利模板")
@RestController
@RequestMapping("/template")
@CrossOrigin(origins = "*", maxAge = 3600)
public class TemplateFileController extends BaseController {
    @Autowired
    private TemplateFileService templateFileService;

    //保存
    @PostMapping("/saveTemplateFile/v1")
    @ApiOperation(value = "上传模板保存", notes = "上传模板保存")
    public Message saveTemplateFile(@RequestBody Map<String,Object> param) {
        try {
            TemplateFile templateFile = StringUtils.toJavaBean(param, TemplateFile.class);
            templateFile.setTemplateFileId(IdGen.generateUUID());
            int i = templateFileService.saveTemplateFile(templateFile);
            if(i>0)
                return Message.success("成功");
            return Message.error(1,"失败");
        }catch (Exception e){
            log.info("上传模板保存失败原因：{}",e.getMessage());
            return Message.error(1,"失败");
        }
    }

    //修改
    @PostMapping("/updateTemplateFile/v1")
    @ApiOperation(value = "上传模板修改", notes = "上传模板修改")
    public Message updateTemplateFile(@RequestBody Map<String,Object> param) {
        try {
            TemplateFile templateFile = StringUtils.toJavaBean(param, TemplateFile.class);
            int i = templateFileService.updateTemplateFile(templateFile);
            if(i>0)
                return Message.success("成功");
            return Message.error(1,"失败");
        }catch (Exception e){
            log.info("上传模板修改失败原因：{}",e.getMessage());
            return Message.error(1,"失败");
        }
    }

    //删除
    @PostMapping("/deleteTemplateFile/v1")
    @ApiOperation(value = "上传模板删除", notes = "上传模板删除")
    public Message deleteTemplateFile(@RequestBody Map<String,Object> param) {
        try {
            if(StringUtils.isNull(param.get("templateFileId")))
                return  Message.error(1,"上传模板删除主键id不能为空");
            String templateFileId=String.valueOf(param.get("templateFileId"));
            int i = templateFileService.deleteTemplateFile(templateFileId);
            if(i>0)
                return Message.success("成功");
            return Message.error(1,"失败");
        }catch (Exception e){
            log.info("上传模板修改失败原因：{}",e.getMessage());
            return Message.error(1,"失败");
        }
    }

    //展示
    @PostMapping("/getTemplateFileList/v1")
    @ApiOperation(value = "上传模板获取", notes = "上传模板获取")
    public Message getTemplateFileList() {
        try {
            List<Map<String,Object>> templateFileList = templateFileService.getTemplateFileList();
                return Message.success("成功",templateFileList);
        }catch (Exception e){
            log.info("上传模板保存失败原因：{}",e.getMessage());
            return Message.error(1,"失败");
        }
    }


}
